package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.bo.MallConfigBo;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.service.AdminConfigService;
import com.cskaoyan.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.Map;

public class AdminConfigServiceImpl implements AdminConfigService {

    @Override
    public MallConfigBo getMallConfig() {

        MallConfigBo mallConfig = new MallConfigBo();

        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        mallConfig.setMarketMallLongitude(marketSystemMapper.selectKeyValueByKeyName("market_mall_longitude"));
        mallConfig.setMarketMallLatitude(marketSystemMapper.selectKeyValueByKeyName("market_mall_latitude"));
        mallConfig.setMarketMallAddress(marketSystemMapper.selectKeyValueByKeyName("market_mall_address"));
        mallConfig.setMarketMallQq(marketSystemMapper.selectKeyValueByKeyName("market_mall_qq"));
        mallConfig.setMarketMallPhone(marketSystemMapper.selectKeyValueByKeyName("market_mall_phone"));
        mallConfig.setMarketMallName(marketSystemMapper.selectKeyValueByKeyName("market_mall_name"));

        session.commit();
        session.close();

        return mallConfig;
    }

    @Override
    public void updateMallConfig(MallConfigBo mallConfigBo) {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        marketSystemMapper.updateKeyValueByKeyName("market_mall_longitude", mallConfigBo.getMarketMallLongitude());
        marketSystemMapper.updateKeyValueByKeyName("market_mall_latitude", mallConfigBo.getMarketMallLatitude());
        marketSystemMapper.updateKeyValueByKeyName("market_mall_address", mallConfigBo.getMarketMallAddress());
        marketSystemMapper.updateKeyValueByKeyName("market_mall_qq", mallConfigBo.getMarketMallQq());
        marketSystemMapper.updateKeyValueByKeyName("market_mall_phone", mallConfigBo.getMarketMallPhone());
        marketSystemMapper.updateKeyValueByKeyName("market_mall_name", mallConfigBo.getMarketMallName());

        session.commit();
        session.close();

        return;
    }

    @Override
    public Map<String, String> getExpressConfig() {
        Map<String, String> expressConfig = new HashMap<>();

        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        expressConfig.put("market_express_freight_min",marketSystemMapper.selectKeyValueByKeyName("market_express_freight_min"));
        expressConfig.put("market_express_freight_value",marketSystemMapper.selectKeyValueByKeyName("market_express_freight_value"));

        session.commit();
        session.close();

        return expressConfig;
    }

    @Override
    public void updateExpressConfig(Map<String,String> marketExpressConfig) {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        marketSystemMapper.updateKeyValueByKeyName("market_express_freight_min",marketExpressConfig.get("market_express_freight_min"));
        marketSystemMapper.updateKeyValueByKeyName("market_express_freight_value",marketExpressConfig.get("market_express_freight_value"));

        session.commit();
        session.close();

        return;

    }

    @Override
    public Map<String, String> getOrderConfig() {
        Map<String, String> orderConfig = new HashMap<>();

        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        orderConfig.put("market_order_unconfirm",marketSystemMapper.selectKeyValueByKeyName("market_order_unconfirm"));
        orderConfig.put("market_order_unpaid",marketSystemMapper.selectKeyValueByKeyName("market_order_unpaid"));
        orderConfig.put("market_order_comment",marketSystemMapper.selectKeyValueByKeyName("market_order_comment"));

        session.commit();
        session.close();

        return orderConfig;

    }

    @Override
    public void updateOrderConfig(Map<String, String> orderConfig) {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        marketSystemMapper.updateKeyValueByKeyName("market_order_unconfirm",orderConfig.get("market_order_unconfirm"));
        marketSystemMapper.updateKeyValueByKeyName("market_order_unpaid",orderConfig.get("market_order_unpaid"));
        marketSystemMapper.updateKeyValueByKeyName("market_order_comment",orderConfig.get("market_order_comment"));

        session.commit();
        session.close();

        return;
    }

    @Override
    public Map<String, String> getWxConfig() {
        Map<String, String> wxConfig = new HashMap<>();

        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        wxConfig.put("market_wx_index_new",marketSystemMapper.selectKeyValueByKeyName("market_wx_index_new"));
        wxConfig.put("market_wx_index_topic",marketSystemMapper.selectKeyValueByKeyName("market_wx_index_topic"));
        wxConfig.put("market_wx_share",marketSystemMapper.selectKeyValueByKeyName("market_wx_share"));
        wxConfig.put("market_wx_index_brand",marketSystemMapper.selectKeyValueByKeyName("market_wx_index_brand"));
        wxConfig.put("market_wx_catlog_goods",marketSystemMapper.selectKeyValueByKeyName("market_wx_catlog_goods"));
        wxConfig.put("market_wx_catlog_list",marketSystemMapper.selectKeyValueByKeyName("market_wx_catlog_list"));
        wxConfig.put("market_wx_index_hot",marketSystemMapper.selectKeyValueByKeyName("market_wx_index_hot"));

        session.commit();
        session.close();

        return wxConfig;
    }

    @Override
    public void updateWxConfig(Map<String, String> wxConfig) {
        SqlSession session = MybatisUtil.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        marketSystemMapper.updateKeyValueByKeyName("market_wx_index_new",wxConfig.get("market_wx_index_new"));
        marketSystemMapper.updateKeyValueByKeyName("market_wx_index_topic",wxConfig.get("market_wx_index_topic"));
        marketSystemMapper.updateKeyValueByKeyName("market_wx_share",wxConfig.get("market_wx_share"));
        marketSystemMapper.updateKeyValueByKeyName("market_wx_index_brand",wxConfig.get("market_wx_index_brand"));
        marketSystemMapper.updateKeyValueByKeyName("market_wx_catlog_goods",wxConfig.get("market_wx_catlog_goods"));
        marketSystemMapper.updateKeyValueByKeyName("market_wx_catlog_list",wxConfig.get("market_wx_catlog_list"));
        marketSystemMapper.updateKeyValueByKeyName("market_wx_index_hot",wxConfig.get("market_wx_index_hot"));

        session.commit();
        session.close();

        return;
    }
}
